<?php

namespace IDock\Insurance\TAIC\Response;

use IDock\Http\Response;
use IDock\Printer\PolicyPaymentPrinter;

class PolicyPayment2Response extends AllTAICResponse
{
    /*** @var PolicyPaymentPrinter $printer */
    public $printer = PolicyPaymentPrinter::class;

    public function resolve(): Response
    {
        $body = $this->getBody();

        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return $this;
        }
        $data = json_decode($body, true);

        if ($this->printer and is_string($this->printer)) {
            $this->printer = new $this->printer;
        }

        $resultCode = $data['resultDTO']['resultCode'] ?? '';
        $resultMess = $data['resultDTO']['resultMess'] ?? '';

        if ($resultCode != 'SUCCESS') {
            $this->setError($resultMess);
            return $this;
        }

        $this->setBody($data);

        return $this;
    }

    public function printerInit(): Response
    {
        $this->printer->payUrl = $this->get('payUrl');

        return $this;
    }

}